package com.bong.ring.view;

import android.content.Context;
import android.support.v4.view.NestedScrollingParentHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by PanXuesen on 2018/2/4.
 */

public class MyNestedScrollParent extends LinearLayout {
    private NestedScrollingParentHelper mParentHelper;

    public MyNestedScrollParent(Context context) {
        super(context);
        init();
    }

    public MyNestedScrollParent(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyNestedScrollParent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mParentHelper = new NestedScrollingParentHelper(this);
    }

    final int myViewIndex = 0, scrollViewIndex = 0;

    View myView;
    View progressView;

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        View temp = ((MyNestedScrollView) getChildAt(myViewIndex)).getChildAt(0);
        myView =  ((RelativeLayout) temp).getChildAt(2);
        progressView = ((RelativeLayout) temp).getChildAt(3);
        View scrollView = getChildAt(scrollViewIndex);
        if (myView instanceof MyView && scrollView instanceof MyNestedScrollView && progressView instanceof ProgressView) {
            ((MyView) myView).isBack(false);
            ((MyView) myView).setProgressView((ProgressView) progressView);
            ((ProgressView) progressView).reset();
        }
        return (nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL);
    }


    @Override
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
        mParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
    }

    @Override
    public void onStopNestedScroll(View target) {
        View scrollView = getChildAt(scrollViewIndex);
        if (myView instanceof MyView && scrollView instanceof MyNestedScrollView) {
            ((MyView) myView).isBack(true);
            ((ProgressView) progressView).beginAnim(true);
        }
        mParentHelper.onStopNestedScroll(target);
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        //consumed[1]=dy;//完全消费y轴的滑动
        //View layout=getChildAt(0);
        View scrollView = getChildAt(scrollViewIndex);
        if (myView instanceof MyView && scrollView instanceof MyNestedScrollView) {
            consumed[1] = 0;
            if (dy < 0) {//下滑
                if (scrollView.getScrollY() == 0)
                    ((MyView) myView).scroll(dy, consumed);
            } else {
                //上滑
                ((MyView) myView).scroll(dy, consumed);
            }
        }
        Log.i("onNestedPreScroll", "dx" + dx + ",dy:" + dy + ",consumed:" + consumed);
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        return false;
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        return false;
    }
}
